<template>
  <j-form-container :disabled="disabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="入库人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="storageName">
              <j-select-user-by-dep v-model="model.storageName" :multi="true" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="入库型号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="storageModel">
              <a-input v-model="model.storageModel" placeholder="请输入入库型号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="入库位置" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="storageLocation">
              <a-input v-model="model.storageLocation" placeholder="请输入入库位置"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="库内件数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="innum">
              <a-input v-model="model.innum" placeholder="请输入库内件数"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="库外件数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="outnum">
              <a-input v-model="model.outnum" placeholder="请输入库外件数"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="库存总数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="totalnum">
              <a-input v-model="model.totalnum" placeholder="请输入库存总数"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="报废件数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="scrapnum">
              <a-input v-model="model.scrapnum" placeholder="请输入报废件数"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="入库时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="storageTime">
              <j-date placeholder="请选择入库时间" v-model="model.storageTime" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"/>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
  </j-form-container>
</template>
<script>
  import { getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
 import { VALIDATE_NO_PASSED } from '@/utils/JEditableTableUtil'
  export default {
    name: 'SysCuttingStorageForm',
    components: {
    },
    props:{
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        validatorRules: {
           storageModel: [
              { required: true, message: '请输入入库型号!'},
              { validator: (rule, value, callback) => validateDuplicateValue('sys_cutting_storage', 'storage_model', value, this.model.id, callback)},
           ],
           innum: [
              { required: false},
              { pattern: /^-?\d+\.?\d*$/, message: '请输入数字!'},
           ],
           outnum: [
              { required: false},
              { pattern: /^-?\d+\.?\d*$/, message: '请输入数字!'},
           ],
           totalnum: [
              { required: false},
              { pattern: /^-?\d+\.?\d*$/, message: '请输入数字!'},
           ],
        },
        confirmLoading: false,
      }
    },
     created () {
      //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods:{
      initFormData(url,id){
        this.clearFormData()
        if(!id){
          this.edit(this.modelDefault)
        }else{
          getAction(url,{id:id}).then(res=>{
            if(res.success){
              let records = res.result
              if(records && records.length>0){
                this.edit(records[0])
              }
            }
          })
        }
      },
      edit(record){
        this.model = Object.assign({}, record)
        console.log("SysCuttingStorageForm-edit",this.model);
      },
      getFormData(){
        let formdata_arr = []
        this.$refs.form.validate(valid => {
          if (valid) {
            let isNullObj = true
            Object.keys(this.model).forEach(key=>{
              if(this.model[key]){
                isNullObj = false
              }
            })
            if(!isNullObj){
              formdata_arr.push(this.model)
            }
          }else{
            this.$emit("validateError","刀具库存表单校验未通过");
          }
        })
        return formdata_arr;
      },
      validate(index){
          return new Promise((resolve, reject) => {
            // 验证主表表单
           this.$refs.form.validate(valid => {
              !valid ? reject({ error: VALIDATE_NO_PASSED ,index}) : resolve()
            })
          }).then(values => {
            return Promise.resolve()
          }).catch(error => {
            return Promise.reject(error)
          })
        },
      clearFormData(){
        this.$refs.form.clearValidate()
      },
    }
  }
</script>
